<?php


namespace app\admin\service;


use app\admin\model\MessageModel;
use app\admin\model\UserModel;
use app\common\Token;

class MessageService
{
    /**
     * 回复留言
     * @param $param
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function answerMessage($param){
        $user_id = Token::getUserIdByToken();
        $result = UserModel::find($user_id);
        if(!$result){
            return false;
        }
        $param['user_name'] = $result->user_name;
        $param['user_email'] = $result->email;
        $param['add_time'] = getDateTime();
        $param['status'] = 1;
        $param['read_status'] = 2;
        $param['is_author'] = 1;
        $messageModel = new MessageModel();
        dbTrans(function () use ($param, $messageModel) {
            $messageModel->updateMessageToReadById($param['parent_id']);
            return $messageModel->save($param);
        });
        return true;
    }
}